package com.person.servlet;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.person.daoImpl.AdminDaoImpl;
import com.person.vo.Article;
import com.person.vo.Comment;

/**
 * Servlet implementation class CommentAddServlet
 */
//@WebServlet("/CommentAddServlet")
public class CommentAddServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CommentAddServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 * 添加评论
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html; charset=UTF-8");
		request.setCharacterEncoding("UTF-8");
		
		HttpSession session = request.getSession();
		
		Article arti = (Article)session.getAttribute("arti");
		session.setAttribute("CommFlag", 0);//评论有新增，需在文章页面重新刷新（获取到最新数据）
		
		String content = request.getParameter("content");//评论内容
		String nickname = request.getParameter("name");//昵称
		String email = request.getParameter("email");//邮件
		String website = request.getParameter("website");//网站
		
		Comment history = new Comment();
		history.setNickName(nickname);
		history.setEmail(email);
		history.setWebsite(website);
		
		
		
		session.setAttribute("history", history);
		
		Date date = new Date();
		
		
		Comment comment = new Comment();
		comment.setCommtentCont(content);
		comment.setNickName(nickname);
		comment.setEmail(email);
		comment.setWebsite(website);
		comment.setDate(date);
		comment.setArticleId(arti.getId());
		
		AdminDaoImpl adi = new AdminDaoImpl();
		boolean flag = adi.commentAdd(comment);
		if(flag) {	//评论添加成功
			System.out.println("评论添加成功！");
			request.getRequestDispatcher("/frontPages/article.jsp").forward(request, response);;
		}else {
			System.out.println("评论添加失败！");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
